<?php
/**
 * Created by PhpStorm.
 * User: wcj
 * Date: 2017/2/14
 * Time: 17:54
 * 我的信息
 */
class MyAction extends CommAction
{
    public function myInfo(){
        $w_id = $this->getUserId();
        $type = $_GET['type'];
        $info = M('student ')
            ->where(array('w_id' => $w_id))
            ->find();
        if($info && $type != '1'){
            redirect(U('myInfo_show'));
        }
        $this->assign('info', $info);
        $this->assign('type', $type);
        //$this->assign('img', json_decode($info['img']));
        //判断此人是否已注册过  已注册跳转至展示页 有修改按钮    未注册   跳转至注册页

        $map['s.w_id']=$w_id;
        $map['s.status']=1;
        $map['s.type']=2;
        $h_info=M("sales_join as s")->join("jl_active as a on a.id=s.a_id")->join("jl_parttime_job as p on s.j_id=p.id ")->join("jl_loupan as l on a.name=l.id")->where($map)->select();
        $this->assign('h_info', $h_info);

       $this->display();
    }
    //已注册的展示页
    public function myInfo_show(){
        $w_id = $this->getUserId();
        $info = M('student ')
            ->where(array('w_id' => $w_id))
            ->find();
       
        $this->assign('info', $info);
        //$this->assign('img', json_decode($info['img']));
        //判断此人是否已注册过  已注册跳转至展示页 有修改按钮    未注册   跳转至注册页

        $map['s.w_id']=$w_id;
        $map['s.status']=1;
        $map['s.type']=2;
        $h_info=M("sales_join as s")->join("jl_active as a on a.id=s.a_id")->join("jl_parttime_job as p on s.j_id=p.id ")->join("jl_loupan as l on a.name=l.id")->where($map)->select();
        $this->assign('h_info', $h_info);
       $this->display();
    }
    //存储用户个人信息
    public function addInfo()
    {
        $data['w_id'] = $this->getUserId();
        $data['name'] = trim(I('get.user_name'));
        $sex = trim(I('get.user_sex'));
        if ($sex == '男') {
            $data['sex'] = 1;
        } else {
            $data['sex'] = 2;
        }
        $addr = explode(' ',I('get.user_address'));
        $data['province'] = $addr[0];
        $data['addr'] = $addr[1];
        $data['district'] = $addr[2];
        $data['phone_num'] = trim(I('get.user_phone'));
        $data['school'] = trim(I('get.user_school'));
        $data['card_num'] = trim(I('get.user_idCard'));
        //$data['img'] = json_encode($_GET['img']);//身份证上传
        $data['ages'] = trim(I('get.ages'));
        $data['bank'] = trim(I('get.user_bankcard'));
        $data['bank_num'] = trim(I('get.user_bankid'));
        $data['bank_phone'] = trim(I('get.user_bankphone'));
        $data['alipay_name'] = trim(I('get.user_ailpayname'));
        $data['alipay_account'] = trim(I('get.user_ailpaynum'));
        $data['finder'] = trim(I('get.user_emergencyName'));
        $data['finder_phone'] = trim(I('get.user_emergencyNum'));
        $data['create_time'] = time();
  //var_dump($data);die;      
        $exist = M('student')->where("w_id=" . $data['w_id'])->find();
        if ($exist) {
            $save = M('student')->where("w_id=" . $data['w_id'])->save($data);
            M('waga')->where("w_id=" . $data['w_id'])->save($data);//数据存储在工资表中
            $this->ajaxReturn(array('res' => 1, 'msg' => '修改成功'));
        } else {
            //$data['w_id'] = $this->getUserId();
            $add = M('student')->add($data);
            M('waga')->add($data);
        }
        if ($add) {
            $this->ajaxReturn(array('res' => 1, 'msg' => '提交成功'));
        } else {
            $this->ajaxReturn(array('res' => 0, 'msg' => '提交失败,请重试'));
        }
    }


    //获取验证码
    public function getCode()
    {
        $code = message($_GET['mobile']);
        if($code){
         $this->ajaxReturn(array('code'=>$code));
        }
    }

    //兼职记录
    public function myInfo2()
    {
        $map['s.a_id'] = I('get.aid');
        //$info = M('active')->where(array('id' => $aid))->find();
        //活动信息
        $map['s.w_id']=$this->getUserId();
        $map['s.status']=1;
        $map['s.type']=2;
        $h_info=M("sales_join as s")->join("jl_active as a on a.id=s.a_id")->join("jl_parttime_job as p on s.j_id=p.id ")->join("jl_loupan as l on a.name=l.id")->where($map)->select();
        // dump($h_info);
        $this->assign('h_info', $h_info);
        //绩效信息
            //关注
        $map1['a_id']=I('get.aid');
        $map1['o_type']=2;
        $map1['w_id']=$this->getUserId();
        $guanzhu=M("record")->where($map1)->sum('num');
            //派发礼品
        $map1['o_type']=1;
        $paifa=M("record")->where($map1)->sum('num');
            //购买
        $map1['o_type']=3;
        $goumai=M("record")->where($map1)->sum('num');
        // dump($guanzhu);dump($paifa);dump($goumai);
        $this->assign('guanzhu', $guanzhu);
        $this->assign('paifa', $paifa);
        $this->assign('goumai', $goumai);
       $this->display();
    }

    //兼职信息列表
    public function parttimeList(){

        $map['status'] = 1;
        $map['end_day'] = array('gt',time()-18*3600);
        $info = M('parttime_job')->where($map)->order('create_time desc')->select();
        $this->assign('info', $info);
        $jobs = M('parttime_job')->where($map)->getField('job_name',true);
        $this->assign('jobs', $jobs);
        $this->display();
    }

    //兼职岗位详情
    public function parttimeDetail()
    {
        $id = I('get.id');
        $info = M('parttime_job')->where(array('id' => $id))->find();
     
            if($info['settlement_method'] == 1){
                $info['settlement_method'] = '次日结';
            }
            if($info['settlement_method'] == 2){
                $info['settlement_method'] = '周结';
            }
            if($info['settlement_method'] == 3){
                $info['settlement_method'] = '月结';
            }
            if($info['education'] == 0){
                $info['education'] = '不限';
            }
            if($info['education'] == 1){
                $info['education'] = '高中';
            }
            if($info['education'] == 2){
                $info['education'] = '大专';
            }
            if($info['education'] == 3){
                $info['education'] = '本科及以上';
            }
            if($info['age'] == 0){
                $info['age'] = '不限';
            }
            if($info['age'] == 1){
                $info['age'] = '20-30';
            }
            if($info['age'] == 2){
                $info['age'] = '30-40';
            }
            if($info['age'] == 3){
                $info['age'] = '40-50';
            }
            if($info['age'] == 4){
                $info['age'] = '50以上';
            }
            if($info['sex'] == 0){
                $info['sex'] = '不限';
            }
            if($info['sex'] == 1){
                $info['sex'] = '男';
            }
            if($info['sex'] == 2){
                $info['sex'] = '女';
            }
            if($info['bonus'] == 1){
                $info['salary'] = $info['salary'] . "/天+提成";
            }
        $map['w_id'] = $this->getUserId();
        $map['j_id'] = I('get.id');
        $status=M("sales_join")->where($map)->field("qd_status")->find();
        $this->assign('info', $info);
        $this->assign('status', $status);
        $this->display();
    }

    //兼职报名
    public function joinActive(){
        header("Content-type: text/html; charset=utf-8");
        $data['w_id'] = $this->getUserId();
        //判断其是否注册,如果未注册,不允许报名
        $is_zhuce = M('student')->where(array('w_id'=>$data['w_id']))->find();
        if(!$is_zhuce){
           echo json_encode(array("msg" => "请先完善个人信息哦","val" => "1"));die;
        }else{
            $data['j_id'] = $_POST['id'];
            $job = M("parttime_job");
            $info = $job->where("id=" . $data['j_id'])->find();
            //判断该岗位是否过期
            $job_end_time = strtotime(date('Y-m-d',$info['end_day']).' '.$info['end_hour']);
            if($job_end_time < time()){
                echo json_encode(array("msg" => "该岗位已失效,看看其他岗位吧","val"=>'4'));die;
            }
            $data['money'] = $info['salary'];
            $data['join_time'] = time();
            $join = M("sales_join");
            $j_join = M("sales_job");
            $exist = $j_join->where(array('w_id'=>$data['w_id'],'j_id'=>$data['j_id']))->find();
            if(!$exist){
                     $data['real_time']=implode("<br/>",$_POST['realtime']);
                     $j_data['real_time']=implode("<br/>",$_POST['realtime']);
                     $j_data['j_id'] = $_POST['id'];
                     $j_data['w_id'] = $this->getUserId();
                     M("sales_job")->add($j_data);
                   $res = $join->add($data);
                    if ($res) {
                        echo  json_encode(array("msg" => "成功","val"=>'2'));
                    }
            }else{
                $old_active_time = explode('<br/>',$exist['real_time']);
                $new_active_time = array_unique(array_merge($old_active_time,$_POST['realtime']));

                  $data['real_time']=implode("<br/>",$new_active_time);
                  $j_data['real_time']=implode("<br/>",$new_active_time);
                  $j_data['j_id'] = $_POST['id'];
                  $j_data['w_id'] = $this->getUserId();
                  M("sales_job")->where(array('w_id'=>$data['w_id'],'j_id'=>$data['j_id']))->save($j_data);
       
                // echo json_encode(array("msg" => "不能重复报名哦!","val"=>'3'));die;
                echo json_encode(array("msg" => "已更新报名时间!","val"=>'3'));die;
            }
        }
        
    }

    //我的任务
    public function myMission()
    {
        $map['s.w_id'] = $this->getUserId();
        //判断是否注册
        $is_zhuce = M('student')->where(array('w_id'=>$this->getUserId()))->find();
        if(!$is_zhuce){
            $this->assign('zhuce','need');
        }
        $map['j.a_id'] = array('neq','0');
        //已完成
        $map['j.type'] = 2;
        //$job_list = M("parttime_job as p")->join("LEFT JOIN jl_sales_join as j ON p.id=j.j_id")->join("LEFT JOIN jl_student as s ON j.w_id=s.w_id")->field("j.id,p.job_name,p.start_day,s.name,s.ages,s.sex,j.status,j.join_time,j.qd_status,s.id as sid,j.j_id")->where($map)->select();
        $job_list = M("active as ac")
                  ->join("LEFT JOIN jl_sales_join as j on j.a_id=ac.id")
                  ->join("jl_student as s on s.w_id=j.w_id")
                  ->field("j.id,s.name,j.status,j.qd_status,ac.create_time")
                  ->where($map)
                  ->order('ac.create_time desc')
                  ->select();
        $this->assign('job_list', $job_list);
        //未完成
        $map['j.type'] = array('lt',2);
        $map['ac.status'] = 1;
        //$job_list1 = M("parttime_job as p")->join("LEFT JOIN jl_sales_join as j ON p.id=j.j_id")->join("LEFT JOIN jl_student as s ON j.w_id=s.w_id")->field("j.id,p.job_name,p.start_day,s.name,s.ages,s.sex,j.status,j.join_time,j.qd_status,s.id as sid,j.j_id")->where($map)->select();
        $job_list1 = M("active as ac")
                  ->join("LEFT JOIN jl_sales_join as j on j.a_id=ac.id")
                  ->join("jl_student as s on s.w_id=j.w_id")
                  ->field("j.id,s.name,j.status,j.qd_status,ac.create_time")
                  ->where($map)
                  ->order('ac.create_time')
                  ->select();
// dump(M()->getLastSql());
//         $job_l=$job_list1;
//         if(array_unique($job_list1)==""){
//             $this->assign('job_list1',$job_l );
//         }else{
//             $this->assign('job_list1',array_unique($job_list1));
//         }
        $this->assign('job_list1',$job_list1 );
        $this->display("mission");
    }
    public function test($j_id,$o_type)
    {
        $l_map['j_id']=$j_id;
        $l_map['o_type']=$o_type;
        $count=M("l_num")->where($l_map)->count();
        if($count==""){
            return false;
        }
        $max=M("l_num")->where($l_map)->max("num");
        $min=M("l_num")->where($l_map)->min("num");
        $sum=M("l_num")->where($l_map)->sum("num");

        $min_max=$max+$min;
        $r_count=$count-2;
        $d=$sum-$min_max;
        return $d/$r_count;
    }
    // 任务详情
    public function Mission_detail()
    {
        $map['id'] = $_GET['id'];
        $info = M("sales_join")->where($map)->find();
        $job_info = M("parttime_job")->where("id=" . $info['j_id'])->find();
        $active_info = M("active")->where("id=" . $info['a_id'])->find();
        $driver_info = M("admin")->where("id=" . $active_info['d_id'])->find();
        $dudao_info = M("admin")->where(array('truename'=>$active_info['dudao']))->find();
        $loupan = M('loupan')->where(array('id'=>$active_info['name']))->find();
        $this->assign('loupan', $loupan);
//var_dump(M()->getLastSql());var_dump($dudao_info);
        $friend = M("sales_job")->where("j_id=" . $info['j_id'])->select();
        foreach ($friend as $vo) {
            $f_id[] = $vo['w_id'];
        }
        $a_id[] = $this->getUserId();
        $s_map['w_id'] = array('in', array_diff($f_id, $a_id));
        $friend_info = M("student")->where($s_map)->select();
        $this->assign('job_info', $job_info);
        $this->assign('active_info', $active_info);
        $this->assign('driver_info', $driver_info);
        $this->assign('dudao_info', $dudao_info);
        $this->assign('friend', $friend_info);
        $this->assign('id', $map['id']);
        $this->assign('qd_status', $info['qd_status']);
        $this->assign('start_img', json_decode($info['start_img']));
        $this->assign('end_img', json_decode($info['end_img']));
        //获取楼盘经纬度
        $loupan_info = M('loupan')->where('id='.$active_info['name'])->find();
        $this->assign('loupan_info', $loupan_info);
        //获取该活动的结束时间  时间结束之前,撤场照片不显示
        $work_day = M('active')->where('id='.$info['a_id'])->getField('create_time');
        $job_end_time1 = date_parse($job_info['end_hour']);
        $work_end_time = $job_end_time1['hour'] *3600 + $job_end_time1['minute'] * 60 - 8*3600;
        $job_end_time = $work_day + $work_end_time;
        if($job_end_time < time()){
            //更改自己已被安排的状态
            $w_id = $this->getUserId();
            $used['used'] = 2;//未安排
            M('student')->where('w_id='.$w_id)->save($used);
            //活动结束,撤场图片显示
            $img_show = 1;
            $this->assign('img_show', $img_show);
            /*$l_n_map['j_id']=$info['j_id'];
            $paifa=$this->test($info['j_id'],1);
            $goumai=$this->test($info['j_id'],2);
            $guanzhu=$this->test($info['j_id'],3);
            if($paifa!=false){
                $l_n_map['o_type']=1;
                $l_n_info=M("l_num")->where($l_n_map)->select();
                foreach ($l_n_info as $key=>$vo){
                    $l_info['j_id']=$info['j_id'];
                    $l_info['l_id']=$vo['l_id'];
                    $num=M("l_num")->where($l_info)->find();
                    $avg_p[$vo['l_id']]=(($num['num']-$paifa)/$paifa)*100+60;
                }
            }
            if($goumai!=false){
                $l_n_map['o_type']=2;
                $l_n_info=M("l_num")->where($l_n_map)->select();
                foreach ($l_n_info as $key=>$vo){
                    $l_info['j_id']=$info['j_id'];
                    $l_info['l_id']=$vo['l_id'];
                    $num=M("l_num")->where($l_info)->find();
                    $avg_g[$vo['l_id']]=(($num['num']-$paifa)/$paifa)*100+60;
                }
            }
            if($guanzhu!=false){
                $l_n_map['o_type']=3;
                $l_n_info=M("l_num")->where($l_n_map)->select();
                foreach ($l_n_info as $key=>$vo){
                    $l_info['j_id']=$info['j_id'];
                    $l_info['l_id']=$vo['l_id'];
                    $num=M("l_num")->where($l_info)->find();
                    $avg_z[$vo['l_id']]=(($num['num']-$paifa)/$paifa)*100+60;
                }
            }
            $jobs=M("parttime_job")->where("id=".$info['j_id'])->find();
            if($avg_p!=""){
                $avg=$avg_g;
                $set_map['aname']=$jobs['job_name'];
                $set_map['type']=1;
                $set=M("jx_pri")->where($set_map)->find();
                if($set['atype']==1) {
                    $p=0.1;
                    $sp=0.9;
                }elseif($set['atype']==2){
                    $p=0.2;
                    $sp=0.8;
                }else{
                    $p=0.3;
                    $sp=0.7;
                }
                foreach ($avg_p as $key=>$vo){
                    $avg_p[$key]=(($vo*$set['pri']/100)*$p);
                }

            }
            if($avg_g!=""){
                $avg=$avg_g;
                $set_map['aname']=$jobs['job_name'];
                $set_map['type']=2;
                $set=M("jx_pri")->where($set_map)->find();
                if($set['atype']==1) {
                    $p=0.1;
                    $sp=0.9;
                }elseif($set['atype']==2){
                    $p=0.2;
                    $sp=0.8;
                }else{
                    $p=0.3;
                    $sp=0.7;
                }
                foreach ($avg_g as $key=>$vo){
                    $avg_g[$key]=(($vo*$set['pri']/100)*$p);
                }
            }
            if($avg_z!=""){
                $avg=$avg_z;
                $set_map['aname']=$jobs['job_name'];
                $set_map['type']=3;
                $set=M("jx_pri")->where($set_map)->find();
                if($set['atype']==1) {
                    $p=0.1;
                    $sp=0.9;
                }elseif($set['atype']==2){
                    $p=0.2;
                    $sp=0.8;
                }else{
                    $p=0.3;
                    $sp=0.7;
                }
                foreach ($avg_z as $key=>$vo){
                    $avg_z[$key]=(($vo*$set['pri']/100)*$p);
                }
            }
            foreach ($avg as $key=>$vo){
                $sum_data[$key]=$avg_g[$key]+$avg_p[$key]+$avg_z[$key];
                    }
            foreach ($sum_data as $key=>$vo){
                $score_l=M("loupan")->where("id=".$key)->find();
                $data['score']=round($score_l['score']*$sp,2)+round($vo,2);
             /*   $datas[$key]=round($score_l['score']*$sp,2)+round($vo,2);*/
               /* M("loupan")->where("id=".$key)->save($data);
            }*/
        }else{
            $img_show = 2;//活动未结束  不显示
            $this->assign('img_show', $img_show);
        }
        $this->assign('ac_time',$_GET['ac_time']);
        $this->display("mission_detail");
    }

    public function qian_dao()
    {
        $where['id'] = $_POST['id'];
        $data['qd_status'] = 1;
        $data['type'] = 1;
        $data['qd_time'] =time();
        $res = M("sales_join")->where($where)->save($data);
        if ($res) {
            echo json_encode(array("ret" => 1, "msg" => "成功"));
        }
    }
    //判断是否签到
    public function check_qiandao(){
        $where['id'] = $_POST['a_id'];
        $qd = M("sales_join")->where($where)->getField('qd_status');
        if($qd == 1){
            echo 1;//已签到
        }else{
            echo 2;//未签到
        }
    }
    //执行记录
    public function record(){
        //判断是否注册
        $is_zhuce = M('student')->where(array('w_id'=>$this->getUserId()))->find();
        if(!$is_zhuce){
            $this->assign('zhuce','need');
        }
        $map['w_id'] = $this->getUserId();
        $map['status'] = 1;
        $map['type'] = 1;
        $info = M("sales_join")->where($map)->find();

        $active = M("active")->where("id=". $info['a_id'])->find();
        
        $o_map['id'] = array('in', json_decode($active['o_id']));
        $order = M("active_jixiao")->where($o_map)->select();
        foreach($order as $k=>$val){
            $where['o_id'] = $val['id'];
            $where['a_id'] = $info['a_id'];
            $where['w_id'] = $map['w_id'];
            $order[$k]['num'] = M('record')->where($where)->sum('num');
        }
        // var_dump($order);
        $student = M("student")->where('w_id=' . $map['w_id'])->find();
        $lou_map['id']=$active['name'];
        $loupan = M("loupan")->where($lou_map)->find();
        $P_map['w_id'] = $this->getUserId();
        $P_map['a_id'] = $active['id'];
        $P_map['o_type'] = 1;
        $P_map['w_id'] = $this->getUserId();
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $g_map['create_time']=array(array('gt',$beginToday),array('lt',time()));
        $paifa=M("record")->where($P_map)->select();
        //关注数量 今日
        $g_map['o_type']=3;
        $g_map['a_id']=$active['id'];
        $g_map['w_id'] = $this->getUserId();
        $g_map['create_time']=array(array('gt',$beginToday),array('lt',time()));
        $guanzhu=M("record")->where($g_map)->select();
        // 购买数量 今日
        $b_map['o_type']=2;
        $b_map['a_id']=$active['id'];
        $b_map['w_id'] = $this->getUserId();
        $b_map['create_time']=array(array('gt',$beginToday),array('lt',time()));
        $goumai=M("record")->where($b_map)->select();
        //派发数量 今日
        
        // die;
        $this->assign('order', $order);
        $this->assign('active', $active);
        $this->assign('student', $student);
        $this->assign('loupan', $loupan);
        $this->assign('paifa', $paifa);
        $this->assign('goumai', $goumai);
        $this->assign('guanzhu', $guanzhu);
        $this->assign('a_id', $info['a_id']);
        $this->display("behavior_record");
    }

    
    public function record_detail()
    {
        if($_GET['r_id']){
            $r_map['id'] = $_GET['r_id'];// 绩效ID
            $info=M("record")->where($r_map)->find();
            $this->assign('info', $info);
            $this->assign('img', json_decode($info['img'],true));
            $this->display("record");
        }else{
            $j_map['id'] = $_GET['id'];// 绩效ID
            $a_id=$_GET['a_id'];
            $info = M("active_jixiao")->where($j_map)->find();
            $a_info=M("active")->where("id=".$a_id)->find();
            $o_id=json_decode($a_info['o_id']);
            $o_map['id']=array("in",$o_id);
        /*    $o_map['type']=1;
            $paifa=M()
            $o_map['type']=2;
            $o_map['type']=3;*/
            $this->assign('info', $info);
            $this->display("behavior_detail");
        }
    }

    public function edit_record()
    {
        $map['id']=$_POST['r_id'];
        $data['num']=$_POST['num'];
        $data['img']=json_encode($_POST['img']);
        $res=M("record")->where($map)->save($data);
        if($res){
            echo json_encode(array("ret"=>1,"msg"=>"成功"));
        }
    }

    public function get_img()
    {
        $a = date('Ymd') . "/";
        $save_dir = "./upload/weixin/" . $a;
        $token = $this->get_token();
        $img_id = $_POST['img_id'];
        $url = "https://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$token&media_id=$img_id";
        $ext = ".jpg";//以jpg的格式结尾
        clearstatcache();//清除文件缓存
        if (trim($url) == '') {
            return array('file_name' => '', 'save_path' => '', 'error' => 1);
        }
        if (trim($save_dir) == '') {
            $save_dir = './';
        }
        //保存文件名
        $filename = time() . $ext;
        if (0 !== strrpos($save_dir, '/')) {
            $save_dir .= '/';
        }
        //创建保存目录
        if (!is_dir($save_dir)) {//文件夹不存在，则新建
            //print_r($save_dir."文件不存在");
            mkdir(iconv("UTF-8", "GBK", $save_dir), 0777, true);
            //mkdir($save_dir,0777,true);
        }
        //获取远程文件所采用的方法

        $ch = curl_init();
        $timeout = 3;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $img = curl_exec($ch);
        curl_close($ch);
        $size = strlen($img);
        //文件大小
        //var_dump("文件大小:".$size);
        $fp2 = @fopen($save_dir . $filename, 'w');
        fwrite($fp2, $img);
        fclose($fp2);
        unset($img, $url);
        if($_POST['type']==1){
            $map['w_id']=$this->getUserId();
            $map['id']=$_POST['id'];
            $info=M("sales_join")->where($map)->find();
            $start_img=json_decode($info['start_img']);
            if($start_img==""){
                $start_img=array($a . $filename);
            }else{
                array_push($start_img, $a . $filename);
            }

            $s_data['start_img']=json_encode($start_img);

            $s_data['start_time']=time();
            $res=M("sales_join")->where($map)->save($s_data);
            if($res){
                $ac = array('ret' => 1, 'file_name' => $filename, 'save_path' => $a . $filename, 'error' => 0);
            }
        }
        if($_POST['type']==2){
            $map['w_id']=$this->getUserId();
            $map['id']=$_POST['id'];
            $info=M("sales_join")->where($map)->find();
            $end_img=json_decode($info['end_img']);
            if($end_img==""){
                $end_img=array($a . $filename);
            }else{
                array_push($end_img, $a . $filename);
            }
            //判断是第几章照片
            if($info['end_img'] == ''){
                $end_img_num = 1;//第一张
            }else{
                $end_img_num = 2;//第一张
                $s_data['type']=2;
            }
            $s_data['end_img']=json_encode($end_img);
            // $s_data['type']=2;
            $s_data['end_time']=time();
            $res=M("sales_join")->where($map)->save($s_data);
            if($res){
                if($end_img_num == 1){
                    $ac = array('ret' => 1, 'file_name' => $filename, 'save_path' => $a . $filename, 'error' => 0);
                }elseif($end_img_num == 2){
                    $ac = array('ret' => 1, 'file_name' => $filename, 'save_path' => $a . $filename, 'error' => 0,'cc'=> 1);
                }
                
            }
        }
        if($_POST['r_id']=="edit"){
                $ac = array('ret' => 1, 'file_name' => $filename, 'save_path' => $a . $filename, 'error' => 0);
        }
        if($_POST['o_id']){
            $ac = array('ret' => 1, 'file_name' => $filename, 'save_path' => $a . $filename, 'error' => 0);
        }
        echo json_encode($ac);
    }

    public function add_record()
    {   
        $data = $_POST;
        $info = M("active_jixiao")->where("id=" . $_POST['o_id'])->find();
        $a_map['w_id']=$this->getUserId();
        $a_map['status']=1;
        $a_map['type']=1;
        $a_map['qd_status']=1;
        $active_info=M("sales_join")->where($a_map)->find();
        $data["o_name"] = $info['name'];
        $data["money"] = $info['cou'];
        $data['a_id'] = $active_info['a_id'];
        $data['w_id'] = $this->getUserId();
        $data['o_type'] = $info['type'];
        $data['img'] = json_encode($_POST['img']);
        $data['create_time'] = time();
        $res = M("record")->add($data);
        $lou=M("active")->where('id=' . $active_info['a_id'])->find();

        $l_data['l_id']=$lou['name'];
        $l_data['j_id']=$active_info['j_id'];
        $l_data['o_type']=$info['type'];
        $rec=M("l_num")->where($l_data)->find();

        if($rec!=null || $rec!=""){
            $l_map['id']=$rec['id'];
            $s_data['num']=$_POST['num']+$rec['num'];
            $s_data['create_time']=time();
            M('l_num')->where($l_map)->save($s_data);
        }else{
            $l_data['num']=$_POST['num'];
            $l_data['create_time']=time();
            M('l_num')->where($l_data)->add($l_data);
        }
        if ($res) {
            echo json_encode(array("ret" => 1, "msg" => "添加记录成功"));
        }
    }

    // 工资显示正式版
    public function salary(){
        $where['w_id'] = $this->getUserId();
        // $where['w_id'] = 72;
        //判断是否注册
        $is_zhuce = M('student')->where(array('w_id'=>$a_map['w_id']))->find();
        if(!$is_zhuce){
            $this->assign('zhuce','need');
        }
        $info1 = M('wage')->where($where)->order('create_time desc')->select();
        
        foreach($info1 as $k=>$val){
            $info[$k]['id'] = $val['id'];
            $info[$k]['w_id'] = $val['w_id'];
            $info[$k]['a_id'] = $val['a_id'];
            $info[$k]['aname'] = M('active')->where(array('id'=>$val['a_id']))->getField('aname');
            $info[$k]['a_createtime'] = M('active')->where(array('id'=>$val['a_id']))->getField('create_time');
            $info[$k]['addr'] = M('active')->where(array('id'=>$val['a_id']))->getField('addr');
            $info[$k]['name'] = $val['name'];
            $info[$k]['card_num'] = $val['card_num'];
            $info[$k]['phone_num'] = $val['phone_num'];
            $info[$k]['alipay_account'] = $val['alipay_account'];
            $info[$k]['jixiao'] = $val['jixiao'];
            $info[$k]['money'] = $val['money']; 
            $info[$k]['status'] = $val['status'];
            $info[$k]['total_money'] = $val['money'] + $val['jixiao'];
            $info[$k]['create_time'] = $val['create_time'];
        }
        $this->assign('info',$info);
        $this->display("wage1");
    }
    public function salary2(){

        $a_map['w_id']=$this->getUserId();
        //判断是否注册
        $is_zhuce = M('student')->where(array('w_id'=>$a_map['w_id']))->find();
        if(!$is_zhuce){
            $this->assign('zhuce','need');
        }
        $a_map['status']=1;
        $a_map['type']=2;
        $a_map['qd_status']=1;
        $active=M("sales_join")->order('end_time desc')->where($a_map)->find();
        //今天开始时间戳
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        //今日结束时间戳
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        //关注数量 今日
        $g_map['o_type']=3;
        $g_map['w_id']=$this->getUserId();
        $g_map['a_id']=$active['a_id'];
        $g_map['status'] = 2;
        $g_map['create_time']=array(array('gt',$beginToday),array('lt',time()));
      $guanzhu=M("record")->where($g_map)->sum('num');
        // 购买数量 今日
        $b_map['o_type']=2;
        $b_map['w_id']=$this->getUserId();
        $b_map['a_id']=$active['a_id'];
        $b_map['status'] = 2;
        $b_map['create_time']=array(array('gt',$beginToday),array('lt',time()));
       $goumai=M("record")->where($b_map)->sum('num');
        //派发数量 今日
        $p_map['o_type']=1;
        $p_map['w_id']=$this->getUserId();
        $p_map['a_id']=$active['a_id'];
        $p_map['status'] = 2;
        $p_map['create_time']=array(array('gt',$beginToday),array('lt',time()));
       $paifa=M("record")->where($p_map)->sum('num');

       
       $g_money= round(salary_g($active['a_id'],$guanzhu),2);
        if($g_money!=""){
            $b_money= round(salary_b($active['a_id'],$goumai)-$g_money,2);
            if($b_money!=""){
                if($paifa==""){
                    $p_money=0;
                }else{
                    $p_money= round(salary_p($active['a_id'],$paifa)-$b_money-$g_money,2);
                }
            }else{
                $p_money=round(salary_p($active['a_id'],$paifa)-$g_money,2);
            }
        }else{
            $b_money= round(salary_b($active['a_id'],$goumai),2);
            if($b_money!=""){
                if($paifa==""){
                    $p_money=0;
                }else{
                    $p_money= round(salary_p($active['a_id'],$paifa)-$b_money,2);
                }
            }else{
                $p_money= round(salary_p($active['a_id'],$paifa)-$b_money,2);
            }
        }
        // $j_money=$p_money+$b_money+$g_money+$active['money'];
        $j_money=$p_money+$b_money+$g_money;
        $w_map['a_id']=$active['a_id'];
        $w_map['j_id']=$active['j_id'];
        $w_map['w_id']=$active['w_id'];
    
        
 // dump($w_map);       
        $res=M("wage")->where($w_map)->find();
        if(!$res){
            $p_info=M("parttime_job")->where("id=".$active['j_id'])->find();
            $s_info=M("student")->where("w_id=".$active['w_id'])->find();
            $w_map['money']=$p_info['salary'];
            $w_map['phone_num']=$s_info['phone_num'];
            $w_map['card_num']=$s_info['card_num'];
            $w_map['bank_num']=$s_info['bank_num'];
            $w_map['jixiao']=$j_money;
            $w_map['total_money']=$p_info['salary'] + $j_money;
            $w_map['name']=$s_info['name'];
            $w_map['create_time']= M('active')->where(array('id'=>$active['a_id']))->getField('create_time');
            $w_map['alipay_account']=$s_info['alipay_account'];
            if($w_map['a_id'] > 0){
                M("wage")->add($w_map);
            }
            
        }else{
            $p_info=M("parttime_job")->where("id=".$active['j_id'])->find();
            $s_info=M("student")->where("w_id=".$active['w_id'])->find();
            $w_map['money']=$p_info['salary'];
            $w_map['phone_num']=$s_info['phone_num'];
            $w_map['card_num']=$s_info['card_num'];
            $w_map['bank_num']=$s_info['bank_num'];
            $w_map['jixiao']=$j_money;
            $w_map['name']=$s_info['name'];
            $w_map['total_money']=$p_info['salary'] + $j_money;
            $w_map['alipay_account']=$s_info['alipay_account'];
            $today['create_time']=array(array('gt',$beginToday),array('lt',$endToday));
            $today['id'] = $res['id'];
            M("wage")->where($today)->save($w_map);
        }

       if($p_money>0 || $b_money>0 || $g_money>0){
        $total_money = $p_money+$b_money+$g_money+$active['money'];
       }else{
        $total_money = 0;
       }
       
       $this->w_salary();
       $this->assign('money', $total_money);
       $this->assign('p_money', $p_money);
       $this->assign('b_money', $b_money);
       $this->assign('g_money', $g_money);
       $this->assign('guanzhu', $guanzhu);
       $this->assign('goumai',  $goumai);
       $this->assign('paifa', $paifa);
        $this->display("wage");
    }
    public function w_salary(){
        $a_map['w_id']=$this->getUserId();
        $a_map['status']=1;
        $a_map['type']=2;
        $a_map['qd_status']=1;
        $active=M("sales_join")->where($a_map)->select();
        //本周开始时间戳
        $week_beg=strtotime(date('Y-m-d', strtotime("this week Monday", time())));
        for($i=0;$i<count($active);$i++){
        //关注数量 本周
            $g_map['o_type']=3;
            $g_map['status'] = 2;
            $g_map['w_id']=$this->getUserId();
            $g_map['a_id']=$active[$i]['a_id'];
            $g_map['create_time']=array(array('gt',$week_beg),array('lt',time()));
            $w_guanzhu[$i]=M("record")->where($g_map)->sum('num');
        //购买数量 本周
            $b_map['o_type']=2;
            $b_map['status'] = 2;
            $b_map['w_id']=$this->getUserId();
            $b_map['a_id']=$active[$i]['a_id'];
            $b_map['create_time']=array(array('gt',$week_beg),array('lt',time()));       
            $w_goumai[$i]=M("record")->where($b_map)->sum('num');
        //派发数量 本周
            $p_map['o_type']=1;
            $p_map['status'] = 2;
            $p_map['w_id']=$this->getUserId();
            $p_map['a_id']=$active[$i]['a_id'];
            $p_map['create_time']=array(array('gt',$week_beg),array('lt',time()));
            $w_paifa[$i]=M("record")->where($p_map)->sum('num');
            $a_money[$i]=$active[$i]['money'];
        }
// dump($w_guanzhu);dump($active);
        for($i=0;$i<count($w_guanzhu);$i++){
            if($w_guanzhu[$i]==null){
                $w_g_money[$i]=0;
            }else{
                $w_g_money[$i]= salary_g($active[$i]['a_id'] ,$w_guanzhu[$i]);
            }
        }
        if($w_goumai==null){
            $w_b_money[$i]= 0;
        }else{
            $w_b_money[$i]= salary_b($active[$i]['a_id'],$w_goumai[$i]);
        }
// dump($w_guanzhu);
        for($i = 0;$i < intval(count($w_paifa));$i++){
            if($w_paifa[$i]==''){
                $w_p_money[$i]=0;
            }else{
                $w_p_money[$i]= salary_g($active[$i]['a_id'] ,$w_paifa[$i]);
            }
        }
        $money=0;//兼职本金
        foreach($a_money as $key => $item){
            $money += $item;
        }
        $g_money=0; // 本周关注绩效奖金总合
        foreach($w_g_money as $item){
            $g_money +=$item;
        }

        $b_money=0;//本周购买绩效奖金总和
        foreach($w_b_money as $item){
            $b_money+=$item;
        }


        $p_money=0;//本周派发绩效奖金总和
        foreach($w_p_money as $item){
            $p_money+=$item;
        }

        $goumai=0;//本周购买数量
        foreach($w_goumai as $item){
            $goumai+=$item;
        }
        $guanzhu=0;//本周关注数量
        foreach($w_guanzhu as $item){
            $guanzhu+=$item;
        }
        $paifa=0;//本周派发数量
        foreach($w_paifa as $item){
            $paifa+=$item;
        }
        
        //本周工资
        $t_map['w_id']=$this->getUserId();
        $t_map['create_time']=array(array('gt',$week_beg),array('lt',time()));
        if($goumai>0 || $paifa>0 || $guanzhu>0){
         $total_money1 = M('wage')->where($t_map)->sum('total_money');
        }else{
         $total_money1 = 0;
        }
// dump($total_money1);dump($p_money);dump($b_money);dump($g_money);
        $this->assign('w_money', $total_money1);
        $this->assign('w_goumai',  $goumai);
        $this->assign('w_paifa',   $paifa);
        $this->assign('w_guanzhu', $guanzhu);
    }



    //用户签到时所需要的操作
    public function get_jw(){
         $district = $_POST['district'];
         $data['latitude'] = $_POST['latitude'];//本人所在的纬度
         $data['longitude'] = $_POST['longitude'];//本人所在的经度
         $data['lat'] = $_POST['lat'];//楼盘所在的纬度
         $data['lng'] = $_POST['lng'];//楼盘所在的经度
         $ac_time = $_POST['ac_time'];
         $today = date('Y-m-d',time());
         if(date('Y-m-d',$ac_time) != $today){
            $this->ajaxReturn(array('msg'=>3));//不在签到日期内
         }
         $dist = $this->sphere_distance($data['latitude'],$data['longitude'],$data['lat'],$data['lng']);
         $formatted = sprintf("%.2f", $dist * 0.612);
         
         if($formatted < 0.5){
            $this->ajaxReturn(array('msg'=>1,"metre"=>$formatted));//在区域内
        }else{
            $this->ajaxReturn(array('msg'=>2,"metre"=>$formatted));//在区域外
        }
         
    }
  
    public function sphere_distance($lat1,$lon1,$lat2,$lon2,$radius=6378.135) {
            $rad = doubleval(M_PI/180.0);

            $lat1 = doubleval($lat1) * $rad;
            $lon1 = doubleval($lon1) * $rad;
            $lat2 = doubleval($lat2) * $rad;
            $lon2 = doubleval($lon2) * $rad;

            $theta = $lon2 - $lon1;
            $dist = acos(sin($lat1) * sin($lat2) + 
                        cos($lat1) * cos($lat2) * cos($theta));
            if($dist < 0) {
              $dist += M_PI;
            }
            // 单位为 千米
            return $dist = $dist * $radius;
  }


}